<?php

namespace Mem;

use MemLib\MemBase;
use MemLib\Pdb;

/**
 * 产品信息表
 *
 * @version 1.0
 * @author 张云江
 */
class MemOrder_Product_info extends MemBase
{
    public function __construct()
    {
        parent::__construct(Pdb::db_order, "t_order_product_info");
    }

    /**
     * 插入数据
     * @param mixed $application_id t_order_application_info 应用ID
     * @param mixed $product_type 1.iOS 2.安卓
     * @param mixed $product_channel 应用支付渠道ID
     * @param mixed $pay 金额
     * @param mixed $state 1.使用中 2.禁止中
     */
    public function insertDataWithParam($application_id, $product_type, $product_channel, $product_name, $product_time, $pay, $state)
    {
        $insert_dt = date("Y-m-d H:i:s", time());
        $data = array(
            "application_id" => $application_id,
            "product_type" => $product_type,
            "product_channel" => $product_channel,
            "product_name" => $product_name,
            "product_time" => $product_time,
            "pay" => $pay,
            "state" => $state,
            "insert_dt" => $insert_dt
        );
        $res = $this->addData($data);
        if ($res) {
            \Mlog::info("t_order_product_info 插入数据成功" . $product_channel);
        }
        return $this->getTmpID();
    }

    /**
     * 获取应用详情
     * @param int $id
     * @return array
     */
    public function getInfoWithid($product_id)
    {
        $product_id = (int)$product_id;
        $sql = "SELECT * FROM t_order_product_info WHERE product_id='{$product_id}';";
        $data = $this->getData($sql, "getInfoWithid:{$product_id}");
        return $data ? $data[0] : array();
    }

    /**
     * 获取应用详情
     * @param int $id
     * @return array
     */
    public function getInfoWithproduct_channel($product_channel)
    {
        $sql = "SELECT * FROM t_order_product_info WHERE product_channel='{$product_channel}';";
        $data = $this->getData($sql, "getInfoWithproduct_channel:{$product_channel}");
        return $data ? $data[0] : array();
    }

    /**
     * 获取应用详情
     * @param int $id
     * @return array
     */
    public function getInfoWithWithApplictionANDproduct_id($product_id)
    {
        $product_id = (int)$product_id;
        $sql = "SELECT pcl.*,pclm.application_name FROM db_order.t_order_product_info AS pcl LEFT JOIN db_order.t_order_application_info AS pclm ON pclm.application_id = pcl.application_id WHERE pcl.product_id='{$product_id}';";
        $data = $this->getData($sql, "getInfoWithWithApplictionANDproduct_id:{$product_id}");
        return $data ? $data[0] : array();
    }

    /**
     * 获取应用详情
     * @return array
     */
    public function getCountDataWithAll()
    {
        $sql = "SELECT COUNT(*) AS total FROM t_order_product_info WHERE is_delete=0;";
        $data = $this->getData($sql, "getCountDataWithAll");
        return empty($data) ? $data : $data[0]['total'];
    }

    /**
     * 获取应用详情
     * @return array
     */
    public function getDataWithAll()
    {
        $sql = "SELECT * FROM t_order_product_info;";
        $data = $this->getData($sql, "getDataWithAll");
        return $data;
    }

    /**
     * 获取应用详情
     * @return array
     */
    public function getDataWithAppliction($mempage)
    {
        $sql = "SELECT pcl.*,pclm.application_name FROM db_order.t_order_product_info AS pcl LEFT JOIN db_order.t_order_application_info AS pclm ON pclm.application_id = pcl.application_id WHERE pcl.product_id != 0 AND pcl.is_delete=0 order by pcl.insert_dt desc LIMIT {$mempage->getLimitIndex()},{$mempage->pagesize};";
        $data = $this->getData($sql, "getDataWithApplictionAndPage:{$mempage->getLimitIndex()}:pagesize:{$mempage->pagesize}");
        return $data;
    }
    /**
     * 更新信息
     * @param $updata
     * @param $wheredata
     * @return bool
     */
    public function updateDataWithParam($updata, $wheredata)
    {
        if (empty($updata) || empty($wheredata)) {
            return false;
        }

        $updata = \MemLib\PublicFunc::convertToHtmlKeyVal($updata);
        $wheredata = \MemLib\PublicFunc::convertToHtmlKeyVal($wheredata);

        $ret = $this->updateData($updata, $wheredata);

        return $ret;
    }

    /**
     * 获取应用详情
     * @return array
     */
    public function DeleteDataWithproduct_id($product_id)
    {
        $product_id = (int)$product_id;
        return $this->deleteData(array("product_id" => $product_id));
    }
}
